#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/11/10 14:42
# @Author  : lizhen
# @Site    : 
# @File    : cIntrospection.py
# @Software: PyCharm
import sys
import builtins
import keyword
import os
from functools import reduce


class Foo(object):
    pass


class Foo2(object):
    def __init__(self):
        self.name = 'abc'

    def func(self):
        return 'ok'


class Singleone(type):
    def __init__(cls, name, bases, dct):
        super(Singleone, cls).__init__(name, bases, dct)
        cls._instance = None

    def __call__(cls, *args, **kwargs):
        if cls._instance is None:
            cls._instance = super(Singleone, cls).__call__(*args, **kwargs)
        return cls._instance


class One(object, metaclass=Singleone):  # python3
    __metaclass__ = Singleone  # python2

    def __init__(self, *args, **kwargs):
        self.name = args[0]


class IntrospectionDemo:
    bar = "introspectionDemo"

    def echo_bar(self):
        print(self.bar)

    def _test_sys(self):
        """
        id = IntrospectionDemo()
        id._test_sys()
        """
        builtins.print(sys.platform)
        builtins.print(sys.version)
        builtins.print(sys.maxsize)
        builtins.print(sys.argv)
        builtins.print(sys.path)
        builtins.print(sys.modules)
        builtins.print(sys.api_version)
        builtins.print(sys.builtin_module_names)
        builtins.print(sys.byteorder)
        builtins.print(sys.base_exec_prefix)
        builtins.print(sys.base_prefix)
        builtins.print(sys.copyright)
        builtins.print(sys.thread_info)
        builtins.print(sys.version_info)
        builtins.print(sys.warnoptions)
        builtins.print(sys.winver)

    def _test_builtins(self):
        """
        id = IntrospectionDemo()
        id._test_builtins()
        """
        builtins.help(builtins.dir)
        builtins.print(builtins.Ellipsis)
        builtins.print(builtins.copyright())
        builtins.print(builtins.globals())
        builtins.print(builtins.locals())
        builtins.print(builtins.NotImplemented)
        builtins.print(builtins.Ellipsis)
        builtins.print(builtins.license())
        builtins.print(builtins.eval('1+1'))
        builtins.print(builtins.list(builtins.filter(lambda x: x > 3, [1, 2, 3])))
        builtins.print(builtins.list(builtins.map(lambda x, y: x + y, [1, 2, 3], [1, 2, 3])))
        addr = object()
        builtins.print(builtins.hash(addr))
        builtins.print(builtins.id(addr))
        builtins.print(builtins.vars())
        builtins.print(builtins.dir())
        builtins.print(builtins.open('conf/cLogging.conf', mode='r'))
        builtins.print(builtins.callable(builtins.list()))
        builtins.print(builtins.isinstance(builtins.list(), builtins.list))
        builtins.print(builtins.issubclass(builtins.list, builtins.object))

    def _test_format(self):
        """
        id = IntrospectionDemo()
        id._test_format()
        """
        print("{}:{}".format('192.168.0.100', 8888))
        print("{server}{1}:{0}".format(8888, '192.168.1.100', server='Web Server Info :'))
        print("{0[0]}.{0[1]}".format(('baidu', 'com')))
        print("{0}*{1}={2: >2}".format(3, 2, 2 * 3))
        print("{:*^30}".format('centered'))

    def _test_keyword(self):
        """
        id = IntrospectionDemo()
        id._test_keyword()
        """
        builtins.print(keyword.kwlist)
        builtins.print(keyword.iskeyword('False'))
        builtins.print(keyword.iskeyword('True'))
        builtins.print(keyword.iskeyword('None'))
        builtins.print(keyword.iskeyword('__debug__'))

    def _test_exit(self):
        """
        id = IntrospectionDemo()
        id._test_exit()
        """
        sys.exit(0)
        os._exit(0)
        builtins.quit(0)
        builtins.exit(0)

    def _test_feature(self):
        """
        id = IntrospectionDemo()
        id._test_feature()
        """
        builtins.print(False)
        builtins.print(True)
        builtins.print(None)
        builtins.print(__debug__)
        builtins.print(__name__)
        builtins.credits()
        builtins.exec('print(1+1)')
        builtins.print(reduce(lambda x, y: x + y, [1, 2, 3], 1))
        builtins.print(os.open('conf/cLogging.conf', os.O_RDONLY))
        builtins.print(hasattr(sys, 'path'))
        builtins.print(getattr(sys, 'path'))

    def _test_metaclass(self):
        """
        id = IntrospectionDemo()
        id._test_metaclass()
        """
        FooChild = type('FooChild', (Foo,), {'bar': True, 'echo_bar': self.echo_bar})
        foochild = FooChild()
        print(foochild.bar)
        foochild.echo_bar()
        one1 = One('a')
        one2 = One('b')
        print(one1.name)
        print(one2.name)
        print(one1.__hash__())
        print(one2.__hash__())

    def _test_reflect(self):
        """
        id = IntrospectionDemo()
        id._test_reflect()
        """
        obj = Foo2()
        ret = getattr(obj, 'func')
        r = ret()
        print(r)
        ret = hasattr(obj, 'func')
        print(ret)
        print(obj.name)
        setattr(obj, 'name', 19)
        print(obj.name)
        print(obj.name)
        delattr(obj, 'name')
        print(hasattr(obj, 'name'))


if __name__ == '__main__':
    id = IntrospectionDemo()
    id._test_reflect()
